/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import e.odbo.data.dao.BaseDAO;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.serialize.serializer.read.SerializerReadBytes;
import com.openbravo.data.loader.serialize.serializer.read.SerializerReadString;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasicExt;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteInteger;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.format.Formats;
import com.openbravo.pos.bean.Resource;
import java.util.List;
import java.util.UUID;

/**
 *
 * @author Administrator
 */
public class ResourceDAO extends BaseDAO<Resource>{
    
    Datas[] resourcedata = new Datas[] {Datas.STRING, Datas.STRING, Datas.INT, Datas.BYTES};

    public ResourceDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return  new TableDefinition(
            "RESOURCES"
            , new String[] {"ID", "NAME", "RESTYPE", "CONTENT"}
            , new String[] {"ID","Label.Name","label.type", "CONTENT"}
            , new Datas[] {Datas.STRING, Datas.STRING, Datas.INT, Datas.BYTES}
            , new Formats[] {Formats.STRING, Formats.STRING, Formats.INT, Formats.BYTEA}
            , new int[] {0}
        );
    }

    @Override
    public void writeInsertValues(DataWrite dp, Resource obj) throws BasicException {
           dp.setString(1,obj.getId()) ;
           dp.setString(2,obj.getName());
           dp.setInt(3, obj.getResType());
           dp.setBytes(4, obj.getContent());
    }

    public Resource readValues(DataRead dr,Resource obj) throws BasicException {
    	if(obj==null)
    		obj=new Resource();
    	obj.setKey(dr.getString(1));
    	obj.setName(dr.getString(2));
    	obj.setResType(dr.getInt(3));
    	obj.setContent(dr.getBytes(4));
        return obj;
    }

   public byte[] getResource(String name) throws BasicException {
        return (byte[]) new PreparedSentence(s
            , "SELECT CONTENT FROM RESOURCES WHERE NAME = ?"
            , SerializerWriteString.INSTANCE
            , SerializerReadBytes.INSTANCE).find(name);
    }

   public final void setResource(String name, int type, byte[] data) throws BasicException {
        Object[] value = new Object[] {UUID.randomUUID().toString(), name, new Integer(type), data};
        if (new PreparedSentence(s
                , "UPDATE RESOURCES SET NAME = ?, RESTYPE = ?, CONTENT = ? WHERE NAME = ?"
                , new SerializerWriteBasicExt(resourcedata, new int[] {1, 2, 3, 1})).exec(value) == 0) {
                 new PreparedSentence(s
                , "INSERT INTO RESOURCES(ID, NAME, RESTYPE, CONTENT) VALUES (?, ?, ?, ?)"
                , new SerializerWriteBasic(resourcedata)).exec(value);
            }
    }

   public final List<String> listResourceNameByType(int type) throws BasicException{
        return new PreparedSentence(s
                , "SELECT NAME FROM RESOURCES WHERE RESTYPE = ?"
            , SerializerWriteInteger.INSTANCE
            , SerializerReadString.INSTANCE).list(type);
    }

   public final List<Resource> listResourceByType(int type) throws BasicException{
        return (List<Resource>) new StaticSentence(s
                ,  "SELECT ID, NAME, RESTYPE, CONTENT FROM RESOURCES WHERE RESTYPE = ?"
                , SerializerWriteInteger.INSTANCE
                , this).list(type);
    }

    @Override
    public Class getSuportClass() {
        return Resource.class;
    }
}
